****************
*LEHD DID REGS *
****************

local controls=""
local outcomelist="anyearn_allst earn " 

cap frame change default
cap frame drop results 
frame create results strL(label) b_evict se_evict b_evictXout se_evictXout n_persons p_person_quarters non_evict_base_mean y_base_n

***************
* Regressions *
***************
foreach pop in "all" "black"  "female"  {
	foreach popval of numlist 0 1 {
		if "`pop'"~="all" | `popval'~=0	{
			foreach per in "1_4" "5_8"  {
				
				if "`per'"=="1_4" local did_est="18_21"
				if "`per'"=="5_8" local did_est="22_25"
				
				if "`per'"=="1_4" local base="13_16"
				if "`per'"=="5_8" local base="9_12"
				
				if "`pop'"~="all" {
					use `outcomelist' evicted qdum* evictXq* pik lehd_yq black hispanic female noprior joint acs_placebo state_ind* using "${build}/linked_lehd_event_study_panel.dta" if evicted~=. & acs_placebo==0 , clear
					gen black_miss = black == .
					gen female_miss = female == . 
					replace black = 0 if black_miss == 1
					replace female = 0 if female_miss == 1
					keep if  `pop'==`popval'
				}
				if "`pop'"=="all" use `outcomelist' evicted qdum* evictXq* pik lehd_yq black hispanic female noprior joint acs_placebo  state_ind* using "${build}/linked_lehd_event_study_panel.dta" if evicted~=. & acs_placebo==0, clear
				
				foreach var of varlist `outcomelist' {
					if "`per'"=="1_4" {
						areg  `var' evicted qdum1-qdum12 qdum17 qdum18_21 qdum22-qdum41 evictXq1-evictXq12  evictXq17 evictXq18_21 evictXq22-evictXq41  `controls' i.lehd_yq, a(pik) cluster(pik) robust
					}
					if "`per'"=="5_8" {
						areg  `var' evicted qdum1-qdum8  qdum13-qdum21 qdum22_25 qdum26-qdum41 evictXq1-evictXq8  evictXq13-evictXq21 evictXq22_25 evictXq26-evictXq41  `controls' i.lehd_yq, a(pik) cluster(pik) robust
					}
						
					local label="Pop: `pop'=`popval', Outcome: `var', Outcome Window: `per'" 
					
					* Save regression output
					local b_evicted= _b[evicted]
					local se_evicted=_se[evicted]
					local b_evictXout=_b[evictXq`did_est']
					local se_evictXout=_se[evictXq`did_est']
					
					unique pik if evicted==0 & qdum`base'==1 & e(sample)
					local y_base_n= r(sum)
					sum `var' if evicted==0 & qdum`base'==1 & e(sample)
					local non_evict_base_mean=r(mean)
					local n_person_quarters=e(N) 

					capture drop first
					capture drop sample
					gen sample=e(sample)
					bys pik sample: gen first=_n==1
					count if first==1 & sample==1
					local n_persons=.
					
					frame post results ("`label'")  (`b_evicted') (`se_evicted') (`b_evictXout') (`se_evictXout') (`n_persons') (`n_person_quarters') (`non_evict_base_mean') (`y_base_n')
					}
				}
			}
	}
}

frame results: export delimited using "${Inputs}/Cook_lehd_did.csv", replace 

import delimited "${Inputs}/Cook_lehd_did.csv", clear 

split label, parse(",")
rename (label1 label2 label3 label4) (population outcome time_period specification)
rename (b_evict se_evict b_evictXout se_evictXout n_obs y_base_mean) (Coeff_evict  SE_evict Coeff_evictXout SE_evictXout N_Person_Quarters Mean_Non_Evict_Base)
drop *judgement*

export delimited using "${Inputs}/Cook_lehd_did.csv", replace 
